package com.gzy.blog.blog.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gzy.blog.blog.dao.DiscussDao;
import com.gzy.blog.blog.entity.Discuss;
import com.gzy.blog.common.BaseDao;
import com.gzy.blog.common.PageModel;

@Repository
public class DiscussDaoImpl extends BaseDao<Discuss, Integer> implements
		DiscussDao {

	@Override
	public List<Discuss> getDiscussListByArticleId(Integer id) {
		// TODO Auto-generated method stub
		return listEntityByHql("SELECT new Discuss(d.id,d.createTime,d.userId,d.content,u.username,u.photo) FROM Discuss d,User u WHERE d.userId=u.id AND d.articleId="
				+ id);
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		Discuss d = get(id);
		delete(d);
	}

	@Override
	public PageModel<Discuss> pageDiscuss(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return pageEntityByHql(
				pageNo,
				pageSize,
				"select new Discuss(d.id,d.content,u.username,a.title) FROM Discuss d,Article a,User u WHERE d.userId=u.id AND d.articleId=a.id");
	}

	@Override
	public void add(Discuss discuss) {
		// TODO Auto-generated method stub
		discuss.setCreateTime(new Date());
		save(discuss);
	}

}
